package c.e.a.b.b.h;

import android.content.ComponentName;
import android.content.Context;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.util.Log;
import b.i.b.f;
import c.e.a.b.b.h.b.i;
import c.e.a.b.b.h.c.a;
import c.e.a.b.e.b.d;
import java.util.HashMap;
import java.util.Objects;
import javax.annotation.concurrent.GuardedBy;

public final class m extends c implements Callback {
    @GuardedBy("mConnectionStatus")
    public final HashMap<a, n> c = new HashMap();
    public final Context d;
    public final Handler e;
    public final c.e.a.b.b.i.a f;
    public final long g;
    public final long h;

    public m(Context context) {
        this.d = context.getApplicationContext();
        this.e = new d(context.getMainLooper(), this);
        this.f = c.e.a.b.b.i.a.a();
        this.g = 5000;
        this.h = 300000;
    }

    public final boolean a(a aVar, ServiceConnection serviceConnection, String str) {
        boolean z;
        f.S(serviceConnection, "ServiceConnection must not be null");
        synchronized (this.c) {
            n nVar = (n) this.c.get(aVar);
            if (nVar == null) {
                nVar = new n(this, aVar);
                aVar.a();
                nVar.a.add(serviceConnection);
                nVar.a(str);
                this.c.put(aVar, nVar);
            } else {
                this.e.removeMessages(0, aVar);
                if (nVar.a.contains(serviceConnection)) {
                    String valueOf = String.valueOf(aVar);
                    StringBuilder stringBuilder = new StringBuilder(valueOf.length() + 81);
                    stringBuilder.append("Trying to bind a GmsServiceConnection that was already connected before.  config=");
                    stringBuilder.append(valueOf);
                    throw new IllegalStateException(stringBuilder.toString());
                }
                c.e.a.b.b.i.a aVar2 = nVar.g.f;
                nVar.e.a();
                nVar.a.add(serviceConnection);
                int i = nVar.b;
                if (i == 1) {
                    ((i) serviceConnection).onServiceConnected(nVar.f, nVar.d);
                } else if (i == 2) {
                    nVar.a(str);
                }
            }
            z = nVar.c;
        }
        return z;
    }

    public final void b(a aVar, ServiceConnection serviceConnection, String str) {
        f.S(serviceConnection, "ServiceConnection must not be null");
        synchronized (this.c) {
            n nVar = (n) this.c.get(aVar);
            String valueOf;
            StringBuilder stringBuilder;
            if (nVar == null) {
                valueOf = String.valueOf(aVar);
                stringBuilder = new StringBuilder(valueOf.length() + 50);
                stringBuilder.append("Nonexistent connection status for service config: ");
                stringBuilder.append(valueOf);
                throw new IllegalStateException(stringBuilder.toString());
            } else if (nVar.a.contains(serviceConnection)) {
                c.e.a.b.b.i.a aVar2 = nVar.g.f;
                nVar.a.remove(serviceConnection);
                if (nVar.a.isEmpty()) {
                    this.e.sendMessageDelayed(this.e.obtainMessage(0, aVar), this.g);
                }
            } else {
                valueOf = String.valueOf(aVar);
                stringBuilder = new StringBuilder(valueOf.length() + 76);
                stringBuilder.append("Trying to unbind a GmsServiceConnection  that was not bound before.  config=");
                stringBuilder.append(valueOf);
                throw new IllegalStateException(stringBuilder.toString());
            }
        }
    }

    public final boolean handleMessage(Message message) {
        int i = message.what;
        a aVar;
        if (i == 0) {
            synchronized (this.c) {
                aVar = (a) message.obj;
                n nVar = (n) this.c.get(aVar);
                if (nVar != null && nVar.a.isEmpty()) {
                    if (nVar.c) {
                        nVar.g.e.removeMessages(1, nVar.e);
                        m mVar = nVar.g;
                        c.e.a.b.b.i.a aVar2 = mVar.f;
                        Context context = mVar.d;
                        Objects.requireNonNull(aVar2);
                        context.unbindService(nVar);
                        nVar.c = false;
                        nVar.b = 2;
                    }
                    this.c.remove(aVar);
                }
            }
            return true;
        } else if (i != 1) {
            return false;
        } else {
            synchronized (this.c) {
                aVar = (a) message.obj;
                n nVar2 = (n) this.c.get(aVar);
                if (nVar2 != null && nVar2.b == 3) {
                    String valueOf = String.valueOf(aVar);
                    StringBuilder stringBuilder = new StringBuilder(valueOf.length() + 47);
                    stringBuilder.append("Timeout waiting for ServiceConnection callback ");
                    stringBuilder.append(valueOf);
                    Log.e("GmsClientSupervisor", stringBuilder.toString(), new Exception());
                    ComponentName componentName = nVar2.f;
                    if (componentName == null) {
                        Objects.requireNonNull(aVar);
                        componentName = null;
                    }
                    if (componentName == null) {
                        componentName = new ComponentName(aVar.b, "unknown");
                    }
                    nVar2.onServiceDisconnected(componentName);
                }
            }
            return true;
        }
    }
}
